//
//  LGECGDetailHeaderView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/8/20.
//

#import "LGECGDetailHeaderView.h"
#import "LGECGDetailChartView.h"

#define  rateW  ((Screen_W - viewPix(44))/3.0)

#define  itemW  (Screen_W/75.0)

@interface LGECGDetailHeaderView()
/**   */
@property (nonatomic , strong)UIScrollView *scrollView;
/**   */
@property (nonatomic , strong)LGECGDetailChartView *chartView;
/**   */
@property (nonatomic , strong)UILabel *timeLabel;
/**   */
@property (nonatomic , strong)UILabel *durationLabel;
/**   */
@property (nonatomic , strong)UILabel *unitLabel;
/**   */
@property (nonatomic , strong)UILabel *aveLabel;
/**   */
@property (nonatomic , strong)UILabel *maxLabel;
/**   */
@property (nonatomic , strong)UILabel *minLabel;

@end

@implementation LGECGDetailHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
    }
    return self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    NSArray *pointArray = dataDic[@"ecg_datas"];
    CGFloat width = pointArray.count*(itemW*8/5.0)>Screen_W ? pointArray.count*(itemW*8/5.0) : Screen_W;
    self.scrollView.contentSize = CGSizeMake(width, itemW*5*6);
    self.chartView.frame = CGRectMake(0, 0, width, itemW*5*6);
    self.chartView.dataArray = pointArray;
    [self.chartView setNeedsDisplay];
    NSString *timeStr = [NSString stringWithFormat:@"采集时间  %@",dataDic[@"record_date"]];
    NSString *durationStr = [NSString stringWithFormat:@"采集时长  %.1f分钟",[dataDic[@"timelength"] integerValue]/60.0];
    self.timeLabel.attributedText = [timeStr attributeString:@{NSFontAttributeName:LGFont(14),NSForegroundColorAttributeName:[UIColor colorWithString:@"#606266"]} range:NSMakeRange(0, 4)];
    self.durationLabel.attributedText = [durationStr attributeString:@{NSFontAttributeName:LGFont(14),NSForegroundColorAttributeName:[UIColor colorWithString:@"#606266"]} range:NSMakeRange(0, 4)];
    self.aveLabel.text = LGNSString(dataDic[@"aveHr"]);
    self.maxLabel.text = LGNSString(dataDic[@"maxHr"]);
    self.minLabel.text = LGNSString(dataDic[@"minHr"]);
}

-(void)createSubView{
    self.backgroundColor = [UIColor clearColor];
    [self addSubview:self.timeLabel];
    [self addSubview:self.durationLabel];
    [self addSubview:self.scrollView];
    [self.scrollView addSubview:self.chartView];
    [self addSubview:self.unitLabel];
    self.aveLabel = [self rateLabelWithIndex:0];
    self.maxLabel = [self rateLabelWithIndex:1];
    self.minLabel = [self rateLabelWithIndex:2];
}

-(UILabel *)timeLabel{
    if(!_timeLabel){
        _timeLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFontWeight(14, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _timeLabel.frame = CGRectMake(viewPix(12), viewPix(10), viewPix(230), viewPix(22));
    }
    return _timeLabel;
}

-(UILabel *)durationLabel{
    if(!_durationLabel){
        _durationLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFontWeight(14, UIFontWeightMedium) textAlignment:NSTextAlignmentRight lines:1];
        _durationLabel.frame = CGRectMake(Screen_W-viewPix(142), viewPix(10), viewPix(130), viewPix(22));
    }
    return _durationLabel;
}

-(UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, viewPix(40), Screen_W, itemW*5*6)];
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.backgroundColor = [UIColor whiteColor];
    }
    return _scrollView;
}

-(LGECGDetailChartView *)chartView{
    if(!_chartView){
        _chartView = [[LGECGDetailChartView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, itemW*5*6)];
        _chartView.itemWidth = (Screen_W/75.0);
        _chartView.segment = 8;
    }
    return _chartView;
}

-(UILabel *)unitLabel{
    if(!_unitLabel){
        _unitLabel = [UILabel lableWithText:@"单位  20mm/s   10mm/mV" colorString:@"#282828" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
        _unitLabel.frame = CGRectMake(viewPix(12), kMaxY(self.scrollView.frame)+viewPix(8), Screen_W-viewPix(24), viewPix(22));
    }
    return _unitLabel;
}

-(UILabel *)rateLabelWithIndex:(NSInteger)index{
    NSArray *titleArray = @[@"平均心率",@"最快心率",@"最慢心率"];
    NSArray *gradientColors = @[@"#E6F9F6",@"#EEE6F9",@"#E6EEF9"];
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(12)+(rateW+viewPix(10))*index, viewPix(85)+itemW*5*6, rateW, viewPix(90))];
    CAGradientLayer *layer = [CAGradientLayer layer];
    layer.frame = CGRectMake(0,0,rateW,viewPix(90));
    layer.startPoint = CGPointMake(0.5, 0);
    layer.endPoint = CGPointMake(0.5, 1);
    layer.colors = @[(__bridge id)[UIColor colorWithString:gradientColors[index]].CGColor, (__bridge id)[UIColor colorWithString:@"#FFFFFF"].CGColor];
    layer.locations = @[@(0), @(0.3f)];
    layer.cornerRadius = 10;
    [baseView.layer addSublayer:layer];
    [baseView setShadoWithColor:@"#B2BDC7" offset:CGSizeMake(0, 2) opacity:0.1 radius:10];
    
    UILabel *titleLabel = [UILabel lableWithText:titleArray[index] colorString:@"#282828" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    titleLabel.frame = CGRectMake(viewPix(12), viewPix(12), rateW-viewPix(20), viewPix(24));
    [baseView addSubview:titleLabel];
    
    UILabel *scoreLabel = [UILabel lableWithText:@"--" colorString:@"#5E8EF5" textFont:LGFontWeight(24, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    [baseView addSubview:scoreLabel];
    
    UILabel *unitLabel = [UILabel lableWithText:@"bmp" colorString:@"#909399" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    [baseView addSubview:unitLabel];
    [scoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(viewPix(10));
        make.left.equalTo(baseView).offset(viewPix(12));
    }];
    [unitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(scoreLabel.mas_right).offset(viewPix(5));
        make.bottom.equalTo(scoreLabel).offset(-3);
    }];
    
    [self addSubview:baseView];
    return scoreLabel;
}

@end
